﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using CMS.Contracts;

namespace Presentation
{
    public partial class ManageStudent : System.Web.UI.Page
        
    {
        Boolean type;
        protected void Page_Load(object sender, EventArgs e)
        {
            type = Convert.ToBoolean(Request["type"]);
                if (!IsPostBack)
                {   
                    if (type==true)
                    {
                       BindClass();
                    }
                    else
                    {
                       BindClass();
                       BindStudentInfo();
                    }
                }
         }

        private void BindClass()
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("CMSClient"))
            {
                IProjectBService channel = channelFactory.CreateChannel();
                ddlClassName.DataSource = channel.GetAllGrades();
                ddlClassName.DataValueField = "ClassID";
                ddlClassName.DataTextField = "ClassName";
                ddlClassName.DataBind();
                this.ddlClassName.Items.Insert(0, "Please Select");
                this.ddlClassName.SelectedIndex = 0;
               
            }
        }

        private void BindStudentInfo()
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("CMSClient"))
            {
                IProjectBService channel = channelFactory.CreateChannel();
                int studentId = Convert.ToInt32(Request["ID"]);
                CMS.Model.Grade grade = new CMS.Model.Grade();
                CMS.Model.Student student = channel.GetStudentById(studentId);
                this.txtStudentName.Text = student.StudentName.Trim();
                if (student.StudentGender.Trim().ToLower() == "female")
                {
                    this.RadioBtnFemale.Checked = true;
                }
                else
                {
                    this.RadioBtnMale.Checked = true;
                }
                this.ddlClassName.SelectedValue = student.Grade.ClassId.ToString().Trim();
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        { 

                   if (type == true)
                    {
                        InsertStudentInfo();
                    }
                    else
                    {
                        UpdateStudentInfo();
                    }
                    Response.Redirect("StudentMainPage.aspx");

                
        }

        private void InsertStudentInfo()
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("CMSClient"))
            {
                IProjectBService channel = channelFactory.CreateChannel();
                CMS.Model.Grade grade = new CMS.Model.Grade();
                CMS.Model.Student student = new CMS.Model.Student(grade);
                student.StudentName = txtStudentName.Text.Trim();
                student.StudentGender = (RadioBtnFemale.Checked == true) ? RadioBtnFemale.Text.Trim() : RadioBtnMale.Text.Trim();
                student.Grade.ClassId = int.Parse(ddlClassName.SelectedValue.ToString());
                channel.AddStudent(student);
            }
        }

        private void UpdateStudentInfo()
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("CMSClient"))
            {
                IProjectBService channel = channelFactory.CreateChannel();
                CMS.Model.Grade grade = new CMS.Model.Grade();
                CMS.Model.Student stu = new CMS.Model.Student(grade);
                stu.StudentName = txtStudentName.Text.Trim();
                stu.StudentGender = (RadioBtnFemale.Checked == true) ? RadioBtnFemale.Text.Trim() : RadioBtnMale.Text.Trim();
                stu.Grade.ClassId = int.Parse(ddlClassName.SelectedValue);
                stu.StudentId = Convert.ToInt16(Request["ID"]);
                channel.UpdateStudent(stu);
            }
        }
    }
}